package com.ay.wiki_planet.service;

import com.ay.wiki_planet.domain.Category;
import com.ay.wiki_planet.domain.CategoryExample;
import com.ay.wiki_planet.mapper.CategoryMapper;
import com.ay.wiki_planet.req.CategoryQueryReq;
import com.ay.wiki_planet.req.CategorySaveReq;
import com.ay.wiki_planet.resp.CategoryQueryResp;
import com.ay.wiki_planet.resp.PageResp;
import com.ay.wiki_planet.util.CopyUtil;
import com.ay.wiki_planet.util.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class CategoryService {

    @Resource
    private CategoryMapper categoryMapper;


    @Resource
    private SnowFlake snowFlake;

    public PageResp<CategoryQueryResp> list(CategoryQueryReq req) {

        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        //动态sql 判断名字不为空则加上条件查询
        if (!ObjectUtils.isEmpty(req.getName())) {
            criteria.andNameLike("%" + req.getName() + "%");
        }
        PageHelper.startPage(req.getPage(), req.getSize());
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);


        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        log.info("总行数:{}", pageInfo.getTotal());
        log.info("总页数:{}", pageInfo.getPages());
        //pageInfo.getTotal(); 总行数
        //pageInfo.getPages(); 总页数

        //将List<Category>变成List<CategoryResp>
        //List<CategoryResp> respList = new ArrayList<>();
        //for(Category category : categoryList){
        //    //实现对象的复制
        //    //CategoryResp categoryResp = new CategoryResp();
        //    //BeanUtils.copyProperties(category,categoryResp);
        //
        //    CategoryResp categoryResp = CopyUtil.copy(category, CategoryResp.class);
        //    respList.add(categoryResp);
        //}


        //列表复制
        List<CategoryQueryResp> list = CopyUtil.copyList(categoryList, CategoryQueryResp.class);

        PageResp<CategoryQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    /*
    * 查询全部数据单独接口 无需分页
    * */
    public List<CategoryQueryResp> all() {

        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        CategoryExample.Criteria criteria = categoryExample.createCriteria();

        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        //列表复制
        List<CategoryQueryResp> list = CopyUtil.copyList(categoryList, CategoryQueryResp.class);
        return list;
    }

    /*
     * 保存
     * */
    public void save(CategorySaveReq req) {
        Category category = CopyUtil.copy(req, Category.class);
        if (ObjectUtils.isEmpty(req.getId())) {
            //新增
            category.setId(snowFlake.nextId());
            categoryMapper.insert(category);
        } else {
            //更新
            categoryMapper.updateByPrimaryKey(category);
        }
    }

    /*
     * 删除
     * */
    public void delete(Long id) {
        categoryMapper.deleteByPrimaryKey(id);
    }


}
